//加密算法
function encryption(pwd) {
    return CryptoJS.enc.Base64.stringify(CryptoJS.SHA256(pwd));
}

//错误处理
function errorHandle(title, error) {
    console.error("发生错误: " + title);
    console.error(`${title}: ${error}`);
    alert(`发生了一些错误:${title},\n请按F12打开控制台Console查看`);
}

function getTableKeys() {
    let ts = {};
    $.each(tableKey, (k, v) => ts[v.t] ? (ts[v.t] += `<th>${v.n}</th>`) : (ts[v.t] = `<tr><th>${v.n}</th>`));
    $.each(ts, (k, v) => $('.tKey_' + k).html(v + '</tr>'));
}

//获取所有猫咪信息
function getAllCat() {
    $.ajax({
        url: "/cats/get/all",
        success: (responseJSON) => {
            console.log(responseJSON);
            if (!responseJSON.success) return errorHandle('allCatResultError', responseJSON.error);
            let r = responseJSON.result;
            let out = document.getElementById('allCatResult');
            for (i = 1, li = out.rows.length; i < li; i++) out.deleteRow(1);
            for (i = 0, li = r.length; i < li; i++) {
                let row = out.insertRow(i + 1);
                let ks = Object.keys(r[i]);
                for (j = 0, lj = ks.length; j < lj; j++) {
                    let cell = row.insertCell(j);
                    cell.innerText = getRealData(ks[j], r[i][ks[j]]).data;
                }
            }
        }
    });
}

function getTypeCat() {
    let out = document.getElementById('typeCatResult');
    let r = getTableNameValues('basic_info', 'color');
    let i;
    $.each(r, (k, v) => {
        let row = out.insertRow(0);
        row.insertCell(0).innerText = k;
        row.insertCell(0).innerText = v;
    });
}

//登录
function login(name, pwd) {
    let time = new Date().getTime();
    pwd = encryption(encryption(pwd) + time);
    $.post("/user/auth", { "username": name, "password": pwd, "time": time },
        (result, statue) => {
            if (result.success) localStorage.setItem("token", result.message);
            else {
                alert(result.message);
                console.log(result.message);
            }
        }
    );
}

//上传
function upload() {
    console.log(this);
    let file = document.getElementById("uploadFile").files[0];
    if (!file) return alert("未选中任何文件");
    var fr = new FileReader();
    fr.onload = () => {
        $.ajax({
            type: "POST",
            url: '/upload/cat-img',
            cache: 'false',
            contentType: 'application/file',
            data: fr.result,
            beforeSend: req => {
                req.setRequestHeader("x-access-token", localStorage.getItem("token"));
                req.setRequestHeader("x-file-size", file.size);
                req.setRequestHeader("x-file-type", file.type);

                req.setRequestHeader("Connection", 'keep-alive');
                req.setRequestHeader("Content-Type", 'application/octet-stream');
            }
        });
    };
    fr.readAsBinaryString(file);
}

function test__cat__create() {
    $.ajax({
        type: "POST",
        url: "/cats/create",
        cache: 'false',
        data: { b: '哈哈', e: 1, k: '2002-01-18', j: '2020-01-01' },
        beforeSend: req => {
            req.setRequestHeader("x-access-token", localStorage.getItem("token"));
        }
    });
}

function test__cat__update() {
    $.ajax({
        type: "POST",
        url: "/cats/update",
        cache: 'false',
        data: { a: 21, b: '哈哈', e: 1, k: '2002-01-18', j: '2020-01-01' },
        beforeSend: req => {
            req.setRequestHeader("x-access-token", localStorage.getItem("token"));
        }
    });
}

function test__cat__remove() {
    $.ajax({
        type: "GET",
        url: "/cats/remove/啊21",
        cache: 'false',
        beforeSend: req => {
            req.setRequestHeader("x-access-token", localStorage.getItem("token"));
        }
    });
}

function test__notice__get() {
    $.ajax({
        type: "GET",
        url: "/notice/get/0/false",
        cache: 'false',
        beforeSend: req => {
            req.setRequestHeader("x-access-token", localStorage.getItem("token"));
        },
        success: res => {
            console.log(res);
        }
    });
}

function test__user__get() {
    $.ajax({
        type: "GET",
        url: "/user/getUser/all/yuanlu/0",
        cache: 'false',
        beforeSend: req => {
            req.setRequestHeader("x-access-token", localStorage.getItem("token"));
        },
        success: res => {
            console.log(res);
        }
    });
}

$(document).ready(() => {
    getTableKeys();
});